import json

from django.shortcuts import render, HttpResponse

from utils.token import Token
from .functions.xftclass import XftInterface
import xftinterfaces.functions.ehrsalaryclass as ehrsalaryclass


# Create your views here.
def employee_info_search(request):
    employee_info = XftInterface()
    employee_info.employeeinfosearch()
    return HttpResponse(123)


def tax_calc_create(request):
    employee_info = XftInterface()
    employee_info.taxcalccreate()
    return HttpResponse(123)


def tax_calc(request):
    employee_info = XftInterface()
    employee_info.taxcalc()
    return HttpResponse(123)


def tax_search(request):
    employee_info = XftInterface()
    employee_info.taxsearch()
    return HttpResponse(123)


# 查询当月未有薪福通序号的人并将薪福通序号写入ehr
def check_stfseq(request):
    ehrsalaryclass.check_stfseq('2022年11月')
    return HttpResponse(1)


def check_all_stfseq(request):
    token = request.headers['token']
    return_data = ehrsalaryclass.check_all_stfseq(token)
    return HttpResponse(json.dumps(return_data))


# 创建核算批次
def calc_salary(request):
    token = request.headers['token']
    request_data = json.loads(request.body.decode('utf-8'))
    return_data = ehrsalaryclass.calc_salary(token, request_data['period'])
    return HttpResponse(json.dumps(return_data))


# 查询核算批次列表
def tax_calc_search(request):
    employee_info = XftInterface()
    employee_info.taxcalcsearchObj = {
        "TXQRYBLSX": [
            {
                "BEGCRE": "2021-01-01",
                "ENDCRE": "2022-12-31",

            }
        ]
    }
    employee_info.taxcalcsearch()
    return HttpResponse(1)


# 算税批次删除
def tax_calc_delete(request):
    employee_info = XftInterface()
    employee_info.taxCalcDeleteObj = {
        "TXCLTBATX": [
            {
                "BATPID": "BAT000000001",
            }
        ]
    }

    employee_info.taxcalcdelete()
    return HttpResponse(1)


# 核算明细结果查询
def search_salary(request):
    token = request.headers['token']
    request_data = json.loads(request.body.decode('utf-8'))
    return_data = ehrsalaryclass.search_salary(token, request_data['period'])
    return HttpResponse(json.dumps(return_data))


# ehr个税结果写入ehr
def set_tax_to_ehr(request):
    token = request.headers['token']
    request_data = json.loads(request.body.decode('utf-8'))
    return_data = ehrsalaryclass.set_tax_to_ehr(token, request_data['period'])
    return HttpResponse(json.dumps(return_data))
